<?php

class VietNamNet {

    var $links = array();
    var $title = "";
    var $intro = "";
    var $content = "";
    var $imgurl = "";

    function Init() {
        $this->title = "";
        $this->intro = "";
        $this->content = "";
        $this->imageurl = "";
    }

    function GetLink($link) {
        $link = trim($link);
        $document = TextBetween('<td align="left" valign="top" class="updated_time_local">', '<a href="#top">', FetchDataString('http://vietnamnet.vn/cntt/'));

        preg_match_all('/<a href="(.*?)"/i', $document, $result, PREG_PATTERN_ORDER);

        $links = array();

        $n = sizeof($result[1]);

        for ($i = 0; $i < $n; $i++) {
            $flag = 0;
            for ($j = $i + 1; $j < $n; $j++)
                if ($result[1][$j] == $result[1][$i])
                    $flag = 1;

            if ($flag == 0)
                array_push($this->links, 'http://vietnamnet.vn' . $result[1][$i]);
        }

        return true;
    }

    function GetInfo($link) {
        $this->Init();

        $document = Unicode_Decode(FetchDataString($link));

        $p1 = strpos($document, '<TD align="left" valign="top" class="tintop_title">');
        $p2 = strpos($document, '<img id="stat_img"');

        $document = substr($document, $p1, $p2 - $p1);

        $search = array('% ?size="?2"?%i', '%<img(.*?)src="/common/v3/images/vietnamnet.gif"(.*?)>%i', '/<a href="(?!http)(.*?)">/i');
        $replace = array('', '', '<a href="http://vietnamnet.vn\1" target="_top">');

        $document = preg_replace($search, $replace, $document);

        if (!preg_match('%<td align="left" valign="top" class="tintop_title">(.*)</td>%i', $document, $result))
            return false;

        $this->title = $result[1];

        if (!preg_match('/<img.*?src="(.*(?:jpg|gif|png|bmp))".*?>/i', $document, $regs))
            $this->imgurl = "";
        else
            $this->imgurl = $regs[1];

        $i = 0;

        while (trim(strip_tags($this->intro)) == "") {
            if (preg_match_all('%<strong>(.*?)</strong>%i', $document, $regs))
                $this->intro = $regs[1][$i++];
        }

        $p = $this->intro;
        $this->intro = strip_tags($this->intro);
        $this->content = TextBetween($p, '</TD></TR></TABLE>', $document);

        return true;
    }

}

?>